package cn.ljy.uem.web.action.backstage.school;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelWriter;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.query.SysUserQuery;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.model.result.ExcelImportResult;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.common.utils.file.MyFileUpload;
import cn.ljy.common.utils.table.MyTableUtil;
import cn.ljy.uem.model.SchoolTerm;
import cn.ljy.uem.model.query.SchoolTermQuery;
import cn.ljy.uem.service.SchoolTermExcelService;
import cn.ljy.uem.service.SchoolTermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 学期控制器
 *
 * @author 凡渊
 * @date 2022/07/18 10:29:37
 */
@Api(tags = "后台的学期模块")
@RestController
@RequestMapping(value = "/api/backstage/school/schoolterm")
public class SchoolTermController extends BaseBackstageController<SchoolTerm, SchoolTermQuery, Integer> {
    @Autowired
    private SchoolTermService baseService;
    @Autowired
    private SchoolTermExcelService termExcelService;
    @ApiOperation(value = "查询所有学期")
    @Override
    @GetMapping("/all")
    protected ResponseData query(SchoolTermQuery query) {
        return super.query(query);
    }

    @ApiOperation(value = "分页查询学期列表")
    @Override
    protected ResponseData query(Integer page, Integer pageSize, SchoolTermQuery query) {
        return super.query(page, pageSize, query);
    }

    @ApiOperation(value = "添加学期")
    @Override
    protected ResponseData add(@Valid @RequestBody SchoolTerm schoolTerm) {
        return super.add(schoolTerm);
    }

    @ApiOperation(value = "批量删除指定的多个学期")
    @Override
    protected ResponseData deletes(@NotEmpty(message = "请选择要移除的记录") Integer[] ids) {
        return super.deletes(ids);
    }

    @ApiOperation(value = "编辑学期")
    @Override
    protected ResponseData edit(@Valid @RequestBody SchoolTerm schoolTerm) {
        return super.edit(schoolTerm);
    }


//    拓展 ============================================================================
@ApiOperation(value = "导入账户表excel数据")
@PostMapping("/excel")
public ResponseData uploadExcel(MultipartFile file) throws Exception{
    if(file!=null&&!file.isEmpty()){
        String fileName=file.getOriginalFilename();//获取文件名
        String extensionName= FileUtil.extName(fileName);//获取文件扩展名
        //验证表格的格式是否正确
        MyFileUpload.validateExtByDir(extensionName,"table");
        //执行表格导入
        ExcelImportResult result=termExcelService.addUsersFromExcel(file.getInputStream(), UserUtil.getLoginUser());
        //String filePath=uploadFolder+Constant.MATERIAL_EXCEL_UPLOAD_PATH;//获取excel上传后保存的物理路径
        return ResponseData.success(result);
    }else{
        throw new MyWebException("操作失败：请选择上传文件");
    }
}

    /**
     * 导出表格模板
     */
    @ApiOperation(value = "导出账户模块的excel数据")
    @GetMapping("/excel")
    public void downloadExcel(HttpServletResponse response, SchoolTermQuery schoolTermQuery){
        ExcelWriter writer=termExcelService.exportExcel(false,schoolTermQuery);
        MyTableUtil.downloadExcelByHutool(response,"学期表格",writer);
    }

    /**
     * 导出表格
     */
    @ApiOperation(value = "导出账户的excel模板")
    @GetMapping("/excel/template")
    public void downloadExcelTemplate(HttpServletResponse response){
        ExcelWriter writer=termExcelService.exportExcel(true,null);
        MyTableUtil.downloadExcelByHutool(response,"学期表格模板",writer);
    }


}
